package cn.wolfcode.web.controller;


import cn.hutool.core.date.DateUtil;
import cn.hutool.db.Session;
import cn.wolfcode.domain.Announcement;
import cn.wolfcode.domain.Appointment;
import cn.wolfcode.domain.JsonResult;
import cn.wolfcode.enums.AnnouncementEnum;
import cn.wolfcode.enums.AppointmentEnum;
import cn.wolfcode.qo.AnnouncementQueryObject;
import cn.wolfcode.service.IAnnouncementService;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.util.RequiredPermission;
import cn.wolfcode.util.UserContext;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/announcement")
public class AnnouncementController {

    @Autowired
    private IAnnouncementService announcementService;


    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") AnnouncementQueryObject qo){
        PageInfo<Announcement> pageInfo = announcementService.query(qo);
        model.addAttribute("pageInfo", pageInfo);

        //所有公告级别
        AnnouncementEnum[] values = AnnouncementEnum.values();
        model.addAttribute("values",values);
        return "announcement/list";
    }


    @RequiredPermission(name="公告管理删除",expression="announcement:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
           announcementService.delete(id);
        }
        return "redirect:/announcement/list";
    }


    @RequiredPermission(name="公告管理新增/编辑",expression="announcement:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Announcement announcement) {
        //设置创建人
        announcement.setCreatePerson(UserContext.getCurrentUser());
        //设置发布时间
        announcement.setCreateTime(DateUtil.date());
        //设置默认发布状态和已读状态
        announcement.setIfread(false);
        announcement.setStatus(false);

        if (announcement.getId() == null) {
            announcementService.save(announcement);
        } else {
            announcementService.update(announcement);
        }
        return "redirect:/announcement/list";
    }


    @RequiredPermission(name="公告管理新增/编辑",expression="announcement:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) {
            model.addAttribute("announcement",announcementService.get(id));
        }
        return "announcement/input";

    }

    @RequestMapping("/listLevels")
    @ResponseBody
    public List<String> listLevels(){
        List<String> list = new ArrayList<>();
        AnnouncementEnum[] values = AnnouncementEnum.values();
        for (AnnouncementEnum value : values) {
            list.add(value.getName());
        }
        return list;
    }


    //操作

    //取消/发布
    @RequestMapping("/change")
    public String  change(Long id,Boolean status){
        Announcement announcement = announcementService.get(id);
        if(status){
            announcement.setStatus(false);
        }else {
            announcement.setStatus(true);
        }
        announcementService.update(announcement);
        return "redirect:/announcement/list";
    }

    //找所有未读公告
    @RequestMapping("/findNoRead")
    @ResponseBody
    public List<Announcement> findNoRead(Model model){

        List<Announcement> announcements = announcementService.selectNoRead();

        return announcements;
    }
}
